(custom-set-variables
 '(helm-gtags-prefix-key "\C-c")
 '(helm-gtags-suggested-key-mapping t))
;; start yasnippet with emacs
(require 'yasnippet)
(yas-global-mode 1)
; to use company-mode in all buffers
(add-hook 'after-init-hook 'global-company-mode)
;;(setq company-idle-delay 0)
; for solving conflicts in Company and Yasnippet
(defun check-expansion ()
    (save-excursion
      (if (looking-at "\\_>") t
        (backward-char 1)
        (if (looking-at "\\.") t
          (backward-char 1)
          (if (looking-at "->") t nil)))))

  (defun do-yas-expand ()
    (let ((yas/fallback-behavior 'return-nil))
      (yas/expand)))

  (defun tab-indent-or-complete ()
    (interactive)
    (if (minibufferp)
		(minibuffer-complete)
		;(ido-complete)
      (if (or (not yas/minor-mode)
              (null (do-yas-expand)))
          (if (check-expansion)
              (company-complete-common)
            (indent-for-tab-command)))))

(global-set-key "\t" 'tab-indent-or-complete)

;;;;;;;;;;;;;;;; irony mode ;;;;;;;;;;;;;;
;; (when (or (eq system-type 'gnu/linux) (eq system-type 'darwin))
;; ;(when (eq system-type 'gnu/linux)
;;   (add-hook 'c++-mode-hook 'irony-mode)
;;   (add-hook 'c-mode-hook 'irony-mode)
;;   (add-hook 'objc-mode-hook 'irony-mode)

;;   ;; replace the `completion-at-point' and `complete-symbol' bindings in
;;   ;; irony-mode's buffers by irony-mode's function
;;   (defun my-irony-mode-hook ()
;; 	(define-key irony-mode-map [remap completion-at-point]
;; 	  'irony-completion-at-point-async)
;; 	(define-key irony-mode-map [remap complete-symbol]
;; 	  'irony-completion-at-point-async)
;; 	)
;;   (add-hook 'irony-mode-hook 'my-irony-mode-hook)
;;   (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
;;   (setq w32-pipe-read-delay 0)
;;   (eval-after-load 'company
;; 	'(add-to-list 'company-backends 'company-irony))
;;   )


;; ;; =============
;; ;; irony-mode
;; ;; =============
;; (add-hook 'c++-mode-hook 'irony-mode)
;; (add-hook 'c-mode-hook 'irony-mode)
;; ;; =============
;; ;; company mode
;; ;; =============
;; (add-hook 'c++-mode-hook 'company-mode)
;; (add-hook 'c-mode-hook 'company-mode)
;; ;; replace the `completion-at-point' and `complete-symbol' bindings in
;; ;; irony-mode's buffers by irony-mode's function
;; (defun my-irony-mode-hook ()
;; (define-key irony-mode-map [remap completion-at-point]
;;   'irony-completion-at-point-async)
;; (define-key irony-mode-map [remap complete-symbol]
;;   'irony-completion-at-point-async))
;; (add-hook 'irony-mode-hook 'my-irony-mode-hook)
;; (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
;; (eval-after-load 'company
;; '(add-to-list 'company-backends 'company-irony))
;; ;; (optional) adds CC special commands to `company-begin-commands' in order to
;; ;; trigger completion at interesting places, such as after scope operator
;; ;;     std::|
;; (add-hook 'irony-mode-hook 'company-irony-setup-begin-commands)
;; ;; =============
;; ;; flycheck-mode
;; ;; =============
;; (add-hook 'c++-mode-hook 'flycheck-mode)
;; (add-hook 'c-mode-hook 'flycheck-mode)
;; (eval-after-load 'flycheck
;; '(add-hook 'flycheck-mode-hook #'flycheck-irony-setup))
;; ;; =============
;; ;; eldoc-mode
;; ;; =============
;; (add-hook 'irony-mode-hook 'irony-eldoc)
;; ;; ==========================================
;; ;; (optional) bind TAB for indent-or-complete
;; ;; ==========================================
;; (defun irony--check-expansion ()
;; (save-excursion
;;   (if (looking-at "\\_>") t
;;     (backward-char 1)
;;     (if (looking-at "\\.") t
;;       (backward-char 1)
;;       (if (looking-at "->") t nil)))))
;; (defun irony--indent-or-complete ()
;; "Indent or Complete"
;; (interactive)
;; (cond ((and (not (use-region-p))
;;             (irony--check-expansion))
;;        (message "complete")
;;        (company-complete-common))
;;       (t
;;        (message "indent")
;;        (call-interactively 'c-indent-line-or-region))))
;; (defun irony-mode-keys ()
;; "Modify keymaps used by `irony-mode'."
;; (local-set-key (kbd "TAB") 'irony--indent-or-complete)
;; (local-set-key [tab] 'irony--indent-or-complete))
;; (add-hook 'c-mode-common-hook 'irony-mode-keys)



(setq-default TeX-engine 'xetex)
(setq-default TeX-PDF-mode t)
;; (add-hook 'LaTeX-mode-hook (lambda()
;; 							 (add-to-list 'TeX-command-list '("XeLaTeX" "%`xelatex%(mode)%' %t" TeX-run-TeX nil t))
;; 							 (setq TeX-command-default "XeLaTeX")
;; 							 (setq TeX-save-query  nil )
;; 							 (setq TeX-show-compilation t)
;; 							 ))


;;;;; AucTeX && SKim begin
(setq TeX-PDF-mode t)
(setq TeX-view-program-list
      '(("PDF Viewer" "/Applications/Skim.app/Contents/SharedSupport/displayline -b -g %n %o %b")))
(eval-after-load 'tex
  '(progn
     (assq-delete-all 'output-pdf TeX-view-program-selection)
     (add-to-list 'TeX-view-program-selection '(output-pdf "PDF Viewer"))))
(add-hook 'LaTeX-mode-hook
          #'(lambda ()
              ;; (add-to-list 'TeX-command-list '("pdfLaTeX" "%`pdflatex -synctex=1%(mode)%' %t" TeX-run-TeX nil t))
			  (add-to-list 'TeX-command-list '("xelaTeX" "%`pdflatex -synctex=1%(mode)%' %t" TeX-run-TeX nil t))
              (setq TeX-command-extra-options "-file-line-error -shell-escape")
              ;;(setq TeX-command-default "pdfLaTeX")
              (setq TeX-save-query  nil ) ;; 不需要保存即可编译
              ))
(custom-set-variables
 '(TeX-source-correlate-method 'synctex)
 '(TeX-source-correlate-mode t)
 '(TeX-source-correlate-start-server t))


;; (setq TeX-auto-save t)
;; (setq TeX-parse-self t)
;; (setq-default TeX-master nil)
;; (add-hook 'LaTeX-mode-hook 'visual-line-mode)
;; (add-hook 'LaTeX-mode-hook 'flyspell-mode)
;; (add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex)
;; (setq reftex-plug-into-AUCTeX t)
;; (setq TeX-PDF-mode t)

;; ;; Use Skim as viewer, enable source <-> PDF sync
;; ;; make latexmk available via C-c C-c
;; ;; Note: SyncTeX is setup via ~/.latexmkrc (see below)
;; (add-hook 'LaTeX-mode-hook (lambda ()
;;   (push
;;     '("latexmk" "latexmk -pdf %s" TeX-run-TeX nil t
;;       :help "Run latexmk on file")
;;     TeX-command-list)))
;; (add-hook 'TeX-mode-hook '(lambda () (setq TeX-command-default "latexmk")))

;; ;; use Skim as default pdf viewer
;; ;; Skim's displayline is used for forward search (from .tex to .pdf)
;; ;; option -b highlights the current line; option -g opens Skim in the background  
;; (setq TeX-view-program-selection '((output-pdf "PDF Viewer")))
;; (setq TeX-view-program-list '(("PDF Viewer" "/Applications/Skim.app/Contents/SharedSupport/displayline -b %n \"%o\" \"%b\"")))
;; (server-start); start emacs in server mode so that skim can talk to
;; it
;; ;; (setq TeX-view-program-list
;;      '(("PDF Viewer" "/Applications/Skim.app/Contents/SharedSupport/displayline -b -g %n %o %b")))
;;;;; AucTeX && SKim end
